import random
import math

class SpecificCharacterProguard:
    def __init__(self, filename, *chars):
        # 展平并处理所有字符参数
        self.chars = []
        for arg in chars:
            if isinstance(arg, str):
                self.chars.extend(list(arg))
            else:
                self.chars.append(str(arg))
        
        if not self.chars:
            raise ValueError("至少需要提供一个混淆字符")
        
        self.filename = filename
        chars_len = len(self.chars)
        self.max_len = max(8, int(math.log(8000) / math.log(chars_len) * 1.5))
        print(f"最大长度：{self.max_len}")

    def start(self):
        generated = set()
        
        # 使用集合和列表分别存储，兼顾效率和顺序
        unique_strings = []
        
        while len(unique_strings) < 8000:
            # 生成随机长度
            str_len = 2 + int(random.random() * self.max_len)
            
            # 构建字符串
            s = []
            for i in range(str_len):
                # 生成随机字符（首字符不能是数字）
                while True:
                    char = random.choice(self.chars)
                    if i > 0 or not char.isdigit():
                        break
                s.append(char)
            
            candidate = ''.join(s)
            
            if candidate not in generated:
                generated.add(candidate)
                unique_strings.append(candidate)
        
        # 批量写入文件
        with open(self.filename, 'w', encoding='utf-8') as f:
            f.write('\n'.join(unique_strings))
        
        print(f"成功生成 {len(unique_strings)} 个唯一字符串到 {self.filename}")

# 使用示例
if __name__ == "__main__":
    # 对应Java的测试用例
    SpecificCharacterProguard("proguard-o0O.txt", '0', 'o', 'O', '8', '〇').start()
    SpecificCharacterProguard("proguard-1il.txt", '1', 'i', 'l', 'L', 'I', '丨').start()
    SpecificCharacterProguard("proguard-socialism.txt", "富强民主文明和谐自由平等公正法治爱国敬业诚信友善").start()